﻿/*****************************************************************************
// *
// * Copyright (c) Microsoft Corporation. All rights reserved.
// * 
// * THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
// * ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
// * IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
// * PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
// * 
// **************************************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using EnvDTE;
using EnvDTE80;
using Extensibility;
using Microsoft.VisualStudio.TeamFoundation;
using Microsoft.VisualStudio.TeamFoundation.VersionControl;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;
using Microsoft.TeamFoundation.VersionControl.Common;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;

namespace Tcvt.Packages
{
    /// <summary>
    /// This class represents the Buddy Build command on the SCE context menu.
    /// </summary>
    class BuddyBuildAddInCommand : SCEAddInCommand
    {
        public BuddyBuildAddInCommand() { }

        /// <summary>
        /// Returns the position on the SCE context menu for this addin command.
        /// </summary>
        protected override int SCEContextMenuPosition
        {
            get
            {
                return s_sceContextMenuPosition;
            }
        }

        /// <summary>
        /// Called when Visual Studio needs to know the status of this command.
        /// </summary>
        public override void QueryStatus(EnvDTE.vsCommandStatusTextWanted neededText, ref EnvDTE.vsCommandStatus status, ref object commandText)
        {
            // We'll only declare that our command is available to be executed if the Source Control Explorer
            // is the currently active window and there is something selected.
            if (IsSCEActive && 
                SourceControlExplorer != null &&
                SourceControlExplorer.SelectedItems.Length > 0)
            {
                status = (vsCommandStatus)vsCommandStatus.vsCommandStatusSupported | vsCommandStatus.vsCommandStatusEnabled;
            }
            else
            {
                status = (vsCommandStatus)vsCommandStatus.vsCommandStatusInvisible;
            }
        }

        /// <summary>
        /// Called when this command is invoked.
        /// </summary>
        public override void Exec(EnvDTE.vsCommandExecOption executeOption, ref object varIn, ref object varOut, ref bool handled)
        {
            handled = true;

            try
            {
                // We'll only get here if QueryStatus returned true, because otherwise the command would be disabled.
                // But we need to double-check our conditions for execution again because things might have changed in
                // the interim.
                if (SourceControlExplorer != null &&
                    SourceControlExplorer.SelectedItems.Length > 0)
                {
                    String[] selectedItems = new String[SourceControlExplorer.SelectedItems.Length];

                    for (int i = 0; i < SourceControlExplorer.SelectedItems.Length; i++)
                    {
                        selectedItems[i] = SourceControlExplorer.SelectedItems[i].SourceServerPath;
                    }

                    // Invoke the dialog on the path(s) selected in the SCE.
                    ShowBuddyBuildDialog(SourceControlExplorer.Workspace);
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message,
                                s_mboxCaption,
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
            }
        }

        /// <summary>
        /// Shows the Buddy Build dialog.
        /// </summary>
        private void ShowBuddyBuildDialog(Workspace workspace)
        {
            // Dynamically load the buddy build package.
            IVsShell shell = Package.GetGlobalService(typeof(SVsShell)) as IVsShell;
            if (shell == null)
            {
                MessageBox.Show("Failed to initialize shell. Please launch command using the BuddyBuild menu.",
                                s_mboxCaption,
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Error);

                return;
            }

            IVsPackage tcvtPackage = null;
            Guid tcvtPackageGuid = new Guid(GuidList.guidTfsBuggerVSPackagePkgString);
            int loadPackageResult = shell.LoadPackage(ref tcvtPackageGuid, out tcvtPackage);

            // Let's display the buddy build window.
            TcvtVsPackage package = tcvtPackage as TcvtVsPackage;

            if (!package.InitializeOptions())
                return;

            MainForm shelvesetsForm = new MainForm();
            shelvesetsForm.ParentPackage = package;
            shelvesetsForm.BuildType = BuildValidationType.BuddyBuild;
            shelvesetsForm.ShowDialog();
            if (shelvesetsForm.BuildSuccessfullyQueued)
            {
                // show the build queue page.
            }
            return;
        }

        public override String Name { get { return "BuddyBuildSCEAddin"; } }
        public override String ButtonText { get { return "Buddy Build..."; } }
        public override String ToolTip { get { return "Queue a check-in validation build (buddy build)."; } }
        public override bool UseMSOIcon { get { return false; } }
        public override int IconIndex { get { return 1; } }

        // Caption for message boxes
        private static readonly String s_mboxCaption = "Buddy Build";

        // Position on the SCE context menu for this addin command.
        private static readonly int s_sceContextMenuPosition = 1;
    }
}